<?PHP
/**
 * htmlencode
 *
 * @param $string string
 * @return string
 */
function htmlencode($string)
{
	$string = str_replace('&', '&amp;', $string);	
	$string = str_replace('<', '&lt;', $string);
	$string = str_replace('>', '&gt;', $string);
	return $string;
}

function htmldecode($string)
{
	$string = str_replace('&lt;', '<', $string);
	$string = str_replace('&gt;', '>', $string);
	$string = str_replace('&amp;', '&', $string);	
	return $string;
}
/**
 * @return string
 * @param $arr arraylist, a hashtable
 * @param $table string the table name
 * @desc return the sql statment for insert...
 */
function getInsertSql($arr, $table)
{
	// not good enough............
	// must be changed........ Bz<binzy@justdn.com> changed it at 2003-9-11
	// next time.........
	$is_first = true;	
	// $Foreach_I = 0;
	$query = "INSERT INTO ".$table." \n(\n";
	$query_key = "";
	$query_val = "";
	// increase the performance.
	foreach($arr as $key=>$val)
	{
		echo $val."<br>";
		if(strlen($val)>0){
			//if($Foreach_I == 0){
			if ($is_first)
			{
				$query_key .= $key;
				if (is_int($val))
				{
					$query_val .= " ".$val."";
				}
				else 
				{
					$query_val .= " '".$val."'";
				}	
				$is_first = false;
			}
			else
			{
				$query_key .= ",\n".$key;
				if (is_int($val))
				{
					$query_val .= ",\n ".$val."";
				}
				else 
				{
					$query_val .= ",\n'".$val."'";
				}

			}
			// $Foreach_I = $Foreach_I + 1;
		}
	}
	$query .=$query_key."\n) \nVALUES \n(\n".$query_val."\n)";
	return $query;
}

/**
 * @return string
 * @param $arr arraylist, a hashtable
 * @param $table string
 * @param $where string 
 * @desc return the sql statment for update...
 */

function getUpdateSql($arr, $table,$where) {
	$is_first = true;	
	$query = "UPDATE ".$table." SET ";
	$query_key = "";
	$query_val = "";
	foreach($arr as $key=>$val) {
		if(strlen($val)>0) {
			if($is_first) {
				$query_key = $key;
				if (is_int($val))
				{
					$query_val = "=".$val."";
				}
				else
				{
					$query_val = "='".$val."'";
				}
				$query .= $query_key.$query_val;
				$is_first = false;
			} else {
				$query_key = ",".$key;
				if (is_int($val))
				{
					$query_val = "=".$val."";
				}
				else
				{
					$query_val = "='".$val."'";
				}
				$query .= $query_key.$query_val;
			}
		}
	}
	$query .= " $where";

	return $query;
}

function print_f($data)
{
	echo '<pre>';
	print_r($data);
	echo '</pre>';
}
/* $Id: Util.php,v 1.3 2005/04/06 04:07:55 icer Exp $ */


class System
{
	function currentTimeMillis()
	{
		list($usec, $sec) = explode(" ",microtime());
		return $sec.substr($usec, 2, 3);
	}

}

class NetAddress
{

	var $Name = 'localhost';
	var $IP = '127.0.0.1';
	
	function getLocalHost() // static
	{
		$address = new NetAddress();
		$address->Name = $_ENV["COMPUTERNAME"];
		$address->IP = $_SERVER["SERVER_ADDR"];

		return $address;
	}

	function toString()
	{
		return strtolower($this->Name.'/'.$this->IP);
	}

}

class Random
{
	function nextLong()
	{
		$tmp = rand(0,1)?'-':'';
		return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
	}
}

class Guid
{

	var $valueBeforeMD5;
	var $valueAfterMD5;

	function Guid()
	{
		$this->getGuid();
	}
// 
	function getGuid()
	{
		$address = NetAddress::getLocalHost();
		$this->valueBeforeMD5 = $address->toString().':'
		.System::currentTimeMillis().':'
		.Random::nextLong();
		$this->valueAfterMD5 = md5($this->valueBeforeMD5);
	}

	function newGuid()
	{
		$Guid = new Guid();
		return $Guid;
	}

	function toString()
	{
		$raw = strtoupper($this->valueAfterMD5);
		return substr($raw,0,8).'-'
		.substr($raw,8,4).'-'
		.substr($raw,12,4).'-'
		.substr($raw,16,4).'-'
		.substr($raw,20);
	}

}
?>